<template>
  <div>
    <div class="page-content">
      <el-form :inline="true" v-model="filterQuery">
        <el-form-item label="角色名称：">
          <el-select
            v-model="filterQuery.sysrole"
            placeholder="请选择角色"
            clearable
            filterable
          >
            <el-option
              v-for="item in options"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery"
            >查询</el-button
          >
          <el-button type="primary" @click="createRoleVisible = true"
            >创建角色</el-button
          >
        </el-form-item>
      </el-form>

      <el-table
        :data="tableData"
        v-loading="loading"
        border
        style="width: 100%"
      >
        <el-table-column
          type="index"
          width="200"
          header-align="center"
          label="序号"
          :index="this.indexStartNum"
          align="center"
        ></el-table-column>
        <el-table-column
          label="角色名称"
          prop="name"
          header-align="center"
          width="500px"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          header-align="center"
          width="900"
        >
          <template slot-scope="scope">
            <div class="inline-td">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="handleEdit(scope.row.id)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDeleteRole(scope.row.id)"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="创建角色"
        :visible.sync="createRoleVisible"
        width="600px"
      >
        <el-form :model="roles" label-width="auto" :rules="rules" ref="roles">
          <el-form-item label="角色名称" prop="name">
            <el-input
              v-model="roles.name"
              autocomplete="off"
              style="width:80%"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="createRoleVisible = false">取 消</el-button>
          <el-button type="primary" @click="cresteSucceedRole('roles')"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <el-dialog title="编辑角色" :visible.sync="editRoleVisible" width="600px">
        <el-form :model="edits" label-width="auto" :rules="rules" ref="edits">
          <el-form-item label="角色名称" prop="name">
            <el-input
              v-model="edits.name"
              autocomplete="off"
              style="width:80%"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editRoleVisible = false">取 消</el-button>
          <el-button type="primary" @click="editSucceedRole('edits')"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <span>此操作将永久删除该条记录, 是否继续?</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitDelet">确 定</el-button>
        </span>
      </el-dialog>
      <div class="pagination-base">
        <el-pagination
          @current-change="handlePageChange"
          :current-page="filterQuery.pageIdx"
          :page-size="filterQuery.pageSize"
          :total="filterQuery.total"
          background
          layout="total, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getQueryAllRole,
  GetRoles,
  CreateRoles,
  GetQueryRole,
  deleteRole,
  EditRole,
  UpdateRole
} from "@/api/module/userManagement";
export default {
  data() {
    return {
      rules: {
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }]
      },
      filterQuery: {
        total: 0,
        pageIdx: 0,
        pageSize: 10,
        sysrole: "",
        name: ""
      },
      roles: {
        name: "",
        id: ""
      },
      edits: {
        name: "",
        id:""
      },
      DeleteUserId: "",
      createRoleVisible: false,
      editRoleVisible: false,
      dialogVisible: false,
      indexStartNum: 1,
      options: [],
      tableData: [],
      loading: false
    };
  },

  mounted() {
    this.getRoleData();
    this.handleQuery();
  },

  methods: {
    // 提交更新
    editSucceedRole() {
      this.editUpdateRole("edits");
    },
    // 编辑用户提交
    editUpdateRole(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(11,this.edits)
          UpdateRole(this.edits).then(
            () => {
              this.editRoleVisible = false;
              this.handleQuery();
              //this.$message.success("操作成功");
              //this.$router.go(-1); //返回上一url
            },
            err => {
              this.$message.error(err.message);
            }
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 编辑用户
    handleEdit(id) {
      this.getUsersEdit(id);
      this.editRoleVisible = true;
    },
    // 点击编辑获取角色详情
    getUsersEdit(id) {
      EditRole(id)
        .then(res => {
          console.log(res, 678);
          const { id, name } = res.data.data;
          this.edits = { id, name };
          this.editRoleVisible = true;
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    // 删除数据
    handleDeleteRole(id) {
      this.DeleteUserId = id;
      this.dialogVisible = true;
      this.handleQuery();
    },
    // 网络请求删除数据
    DeleteRole() {
      deleteRole(this.DeleteUserId)
        .then(res => {
          this.handleQuery(this.filterQuery);
          console.log(res);
        })
        .catch(e => {
          console.log(e);
          this.$message.error("删除失败");
        });
    },
    // 二次确认删除数据
    submitDelet() {
      this.dialogVisible = false;
      this.DeleteRole();
    },
    // 查询
    handleQuery() {
      console.log(this.filterQuery, 33);
      let filterQuery = {
        name: this.filterQuery.sysrole,
        num: this.filterQuery.pageIdx,
        size: this.filterQuery.pageSize
      };
      GetQueryRole(filterQuery).then(res => {
        console.log("rr", this.filterQuery.name);
        this.indexStartNum =
          this.filterQuery.pageIdx * this.filterQuery.pageSize + 1;
        this.tableData = res.data.data.content;
        this.filterQuery.total = res.data.data.totalElements;
      });
    },
    // 创建角色
    cresteSucceedRole(rolesName) {
      console.log("u", this.$refs);
      this.$refs[rolesName].validate(valid => {
        console.log("f", valid);
        if (valid) {
          console.log(this.roles, 13);
          const { name, id } = this.roles;
          CreateRoles({
            name,
            id
          })
            .then(() => {
              this.createRoleVisible = false; //返回上一url
              this.$refs[rolesName].resetFields();
              // this.handleQuery();
            })
            .catch(err => {
              console.log("err", err);
              this.$message.error(err.message);
            });
        } else {
          this.$notify.warning({
            title: "温馨提示",
            message: "请检查必填项"
          });
          return false;
        }
      });
    },
    //页码改变
    handlePageChange(page) {
      this.filterQuery.pageIdx = page;
      this.handleQuery();
    },
    // 获取角色
    getRoleData() {
      // let params = {
      //   label: '角色'
      // }
      GetRoles()
        .then(res => {
          console.log("ss", res);
          this.options = res.data.data;
        })
        .catch(() => {});
    }
  }
};
</script>
<style media="screen">
/*分辨率低于1280，采用下面的样式*/
@media screen and (max-device-width: 1280px) {
  .outDiv {
    width: 1060px;
  }
}
/*分辨率高于1280，采用下面的样式*/
@media screen and (min-device-width: 1280px) {
  .outDiv {
    width: 100%;
  }
}
</style>
